<?php
$this->layout = "//layouts/column1";
?>
<h1>Manage Member Positions</h1>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'member-position-grid',
    'dataProvider' => $dataProvider,
    'columns' => array(
        'no_of_positions',
        array(
            'name' => 'purchase_date',
            'header' => 'Purchage',
            'value' => 'Yii::app()->dateFormatter->format("d MMM y", $data->purchase_date)'
        ),
        array(
            'name' => 'expire_date',
            'header' => 'Expire',
            'value' => 'Yii::app()->dateFormatter->format("d MMM y", $data->expire_date)'
        ),
        array(
            'header' => 'Day Left',
            'value' => 'getDayLeft($data->expire_date)'
        ),
        array(
            'header' => 'Earned Profits',
            'value' => 'getCurrnetProfit($data->purchase_date, $data->no_of_positions)'
        ),
    ),
));
function getDayLeft($futuredate) {
    $d = new DateTime($futuredate);
    return $d->diff(new DateTime())->format('%a days');
}
function getCurrnetProfit($buydate, $pn) {
    $datetime1  = new DateTime();
    $d1 = $datetime1->format('Y-m-d');
    $d1 = new DateTime($d1);
    $datetime2 = new DateTime($buydate);
    $d2 = $datetime2->format('Y-m-d');
    $d2 = new DateTime($d2);
    $dd = $d1->diff($d2)->format('%a');
    $dd = ($dd > 80) ? 80 : $dd;
    $p  = ($dd * $pn * 0.2);
    return '$'.$p;
}
?>
